import filterData from '@/common/filterData';
import mergeData from '@/common/mergeData';
import formatter from '@/common/formatter';

const base = { title: '在途TOP人员' }
const dataset = { source: [] };

function getChartData(data) {
  let chartData = data[2].tousu_zt;
  const names = chartData.map((item, index) => {
    let text = item._id;
    text = text.length > 3 ? '...' + text.slice(-3) : text;
    // if (index % 2 === 0) text = '\n' + text;
    return text;
  });

  const fieldName = ['普通在途', '超长', '预警', 'VIP'];

  const dataset = { source: [['人员', ...fieldName]] },
    series = [];

  chartData = chartData.map((item, index) => (
    [
      names[index],
      item.total - (item.chaochang + item.yujing + item.vip),
      item.chaochang,
      item.yujing,
      item.vip
    ]
  ));
  dataset.source.push(...chartData)

  fieldName.forEach((field, index) => {
    series.push({
      type: 'bar',
      stack: 'focused',
      label: {
        show: index === fieldName.length - 1,
        position: window.isMobile ? 'right' : 'top',
        formatter: (params) => params.data.reduce((acc, cur) => !isNaN(Number(cur)) ? acc + Number(cur) : acc, 0)
      }
    })
  });

  const axisA = {
    type: 'category',
    data: names,
    inverse: window.isMobile,
    // axisLabel: { interval: 0 },
    axisPointer: {
      type: 'shadow'
    }
  },
    axisB = { type: 'value' };

  this.xAxis = window.isMobile ? axisB : axisA;
  this.yAxis = window.isMobile ? axisA : axisB;

  this.dataset = dataset;
  this.series = series;
  this.title.text = `在途TOP${chartData.length}人员`;

}

// ======更新内容↑========

export default {
  title: {
    text: base.title,
    left: 'center'
  },
  legend: {
    type: 'scroll',
    top: 30
  },
  color: ['#5470c6', 'red', 'yellow', 'orange'],
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    }
  },
  grid: {
    width: window.isMobile ? '92%' : '96%',
    height: window.isMobile ? '90%' : '78%',
    left: '2%',
    right: window.isMobile ? '6%' : '2%',
    top: window.isMobile ? '8%' : '20%',
    bottom: '2%',
    containLabel: true
  },
  dataset,
  series: [{ type: 'bar' }],
  getChartData,
}